package com.iman.manjusri.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.iman.common.support.Page;
import com.iman.common.util.ConfigUtil;
import com.iman.manjusri.service.RmtpService;
import com.iman.manjusri.util.HttpRequestor;
import com.iman.manjusri.vo.topics.Topics;
import com.iman.manjusri.vo.topics.TopicsResponse;
import com.iman.manjusri.vo.topics.TopicsVO;

@Service
public class RmtpServiceImpl implements RmtpService {

	@Override
	public Page<TopicsVO> getImageList(int pn, int ps) {
		String serUrl = ConfigUtil.getString("topics.server");
		String drmFileurl = ConfigUtil.getString("drmFile.server");
		serUrl += "?countPerPage=" + ps + "&";
		serUrl += "offset=" + (pn - 1) * ps;
		Page<TopicsVO> pages = new Page<TopicsVO>();
		try {
			String res = new HttpRequestor().doGet(serUrl);
			Gson gson = new Gson();
			TopicsResponse topicsResponse = gson.fromJson(res,
					TopicsResponse.class);
			List<Topics> results = topicsResponse.getResults();

			List<TopicsVO> lists = new ArrayList<TopicsVO>();
			if (results != null && !results.isEmpty()) {
				for (Topics t : results) {
					lists.add(new TopicsVO(t, drmFileurl + "/" + t.getFileId()));
				}
			}
			pages.setData(lists);
			pages.setStart(ps);
			pages.setPageSize(pn);
			pages.setTotalCount(topicsResponse.getTotalCount());
			
			return new Page<TopicsVO>(Page.getStartOfPage(pn, ps),topicsResponse.getTotalCount(),ps,lists);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
